$(function () {
    syb("#signOrgId");
    initData();
    countCertificate();
    loadWarningAccount('[name="warningAccount"]');
    // loadWarningConfigure('[name="warningConfigure"]');
});

/**
 * 更具事业部刷新数据
 */
function refreshSata() {
    var signOrgId = $("#signOrgId").val();
    initData(signOrgId);
    countCertificate(signOrgId);
    loadWarningAccount('[name="warningAccount"]', signOrgId);
    // loadWarningConfigure('[name="warningConfigure"]', signOrgId);
}

/**
 * 初始化数据
 */
function initData(signOrgId) {
    var isAll = null;
    var checkResult = checkHasSybAuth($('#signOrgId option'));
    if (!checkResult) {
        isAll = "0"
    }
    expirationWarning(signOrgId, isAll);
}

//刷新证书量
function certificateQuantity() {

}

//刷新过期预警
function expirationWarning(signOrgId, isAll) {
    $.ajax({
        type: "post",
        url: sysInfo.basePath + "/zs/certificate_statistics/get_expiration_warning.do",
        data: {
            "signOrgId": signOrgId,
            "isAll": isAll
        },
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                var data = result.data;
                if (!data) {
                    $("#certificateExpires").html(0);
                    $("#certificatesAttached").html(0);
                    $("#talentContract").html(0);
                    $("#enterpriseContract").html(0);
                    $("#certificateUnused").html(0);
                } else {
                    $("#certificateExpires").html(data.certificateExpires);
                    $("#certificatesAttached").html(data.certificatesAttached);
                    $("#talentContract").html(data.talentContract);
                    $("#enterpriseContract").html(data.enterpriseContract);
                    $("#certificateUnused").html(data.certificateUnused);
                }
            } else {
                DGG.Errors(result.msg);
            }
        }
    });
}

//跳转页面
function jumpWarning(type) {
    if (type === 1) {
        window.parent.addTab("证书过期预警", sysInfo.basePath + "/zs/expire/warning/index.html");
    } else if (type === 2) {
        window.parent.addTab("证书使用预警", sysInfo.basePath + "/zs/affiliated/warning/index.html");
    } else if (type === 3) {
        window.parent.addTab("人才合同到期预警", sysInfo.basePath + "/zs/talentContract/warning/index.html");
    } else if (type === 4) {
        window.parent.addTab("企业合同到期预警", sysInfo.basePath + "/zs/enterpriseContract/warning/index.html");
    } else if (type === 5) {
        window.parent.addTab("证书未使用预警", sysInfo.basePath + "/zs/unused/warning/index.html");
    }
}

//刷新库容预警
function capacityWarning() {

}

//刷新资金池预警
function capitalPoolWarning() {

}

/**
 * 证书总览统计
 */
function countCertificate(signOrgId) {
    var params = {};
    if (!checkIsEmpty(signOrgId)) {
        params.signOrgId = signOrgId;
    }
    var checkResult = checkHasSybAuth($('#signOrgId option'));
    if (!checkResult) {
        params.isAll = "0"
    }
    $.post(sysInfo.basePath + "/zs/general_credential/general_credential_count.do", params, function (result) {
        if (result.code == 0) {
            $('[name="enteringCount"]').html(!checkIsEmpty(result.data.enteringCount) ? result.data.enteringCount : 0);
            $('[name="count"]').html(!checkIsEmpty(result.data.count) ? result.data.count : 0);
            $('[name="reservationCount"]').html(!checkIsEmpty(result.data.reservationCount) ? result.data.reservationCount : 0);
            $('[name="affiliatedCount"]').html(!checkIsEmpty(result.data.affiliatedCount) ? result.data.affiliatedCount : 0);
            $('[name="overdueAffiliatedCount"]').html(!checkIsEmpty(result.data.overdueAffiliatedCount) ? result.data.overdueAffiliatedCount : 0);
            $('[name="noUseCount"]').html(!checkIsEmpty(result.data.noUseCount) ? result.data.noUseCount : 0);
            $('[name="talentContractCount"]').html(!checkIsEmpty(result.data.talentContractCount) ? result.data.talentContractCount : 0);
            $('[name="companyContractCount"]').html(!checkIsEmpty(result.data.companyContractCount) ? result.data.companyContractCount : 0);
            $('[name="certificateOverDueCount"]').html(!checkIsEmpty(result.data.certificateOverDueCount) ? result.data.certificateOverDueCount : 0);
            $('[name="nationwideCount"]').html(!checkIsEmpty(result.data.nationwideCount) ? result.data.nationwideCount : 0);
            $('[name="localCount"]').html(!checkIsEmpty(result.data.localCount) ? result.data.localCount : 0);
        } else {
            DGG.Errors(result.msg);
        }

    })
}

/**
 * 加载预订信息
 */
function loadWarningAccount(obj, signOrgId) {
    var params = {};
    var checkResult = checkHasSybAuth($('#signOrgId option'));
    if (!checkResult) {
        params.isAll = "0"
    }
    if (!checkIsEmpty(signOrgId)) {
        params.signOrgId = signOrgId;
    }
    $.post(sysInfo.basePath + "/zs/general_credential/wanrning_count.do", params, function (result) {
        if (result.code == 0) {
            var datas = result.data;
            $(obj).empty();
            $(obj).append("<thead>\n" +
                "<tr>\n" +
                "    <th>证书类型</th>\n" +
                "    <th>在库可用量</th>\n" +
                "    <th>预定量</th>\n" +
                "    <th>匹配量</th>\n" +
                "    <th>使用量</th>\n" +
                "    <th>预约量</th>\n" +
                "</tr>\n" +
                "</thead><tbody>");
            if (!checkIsEmpty(datas) && datas.length > 0) {
                $.each(result.data, function (i, item) {
                    $(obj).append("<tr>\n" +
                        "<td>" + item.certificateMajorName + "</td>\n" +
                        "<td>" + item.libraryNumbers + "</td>\n" +
                        "<td>" + item.reserveNumbers + "</td>\n" +
                        "<td>" + item.matchNumbers + "</td>\n" +
                        "<td>" + item.hangUpNumbers + "</td>\n" +
                        "<td>" + item.orderNumbers + "</td>\n" +
                        "</tr>");
                });
                $(obj).append("</tbody>");
            } else {
                $(obj).append("<tr>\n" +
                    "<td colspan='6'><center>没有数据</center></td>\n" +
                    "</tr></tbody>");
            }

        } else {
            DGG.Errors(result.msg);
            return false;
        }
    });
}


/**
 * 加载预订信息
 */
// function loadWarningConfigure(obj, signOrgId) {
//     var params = {};
//     var checkResult = checkHasSybAuth($('#signOrgId option'));
//     if (!checkResult) {
//         params.isAll = "0"
//     }
//     if (!checkIsEmpty(signOrgId)) {
//         params.signOrgId = signOrgId;
//     }
//     $.post(sysInfo.basePath + "/zs/general_credential/wanrning_configure.do", params, function (result) {
//         if (result.code == 0) {
//             var datas = result.data;
//             $(obj).empty();
//             $(obj).append("<thead>\n" +
//                 "<tr>\n" +
//                 "    <th>地区</th>\n" +
//                 "    <th>资金池默认额度（元）</th>\n" +
//                 "    <th>现有资金池额度（元）</th>\n" +
//                 "    <th>收证支款金额（元）</th>\n" +
//                 "    <th>使用收款金额（元）</th>\n" +
//                 "</tr>\n" +
//                 "</thead><tbody>");
//             if (!checkIsEmpty(datas) && datas.length > 0) {
//                 $.each(result.data, function (i, item) {
//                     var nowCapitalPoolWarning = item.nowCapitalPoolWarning > 0 ? (item.nowCapitalPoolWarning > item.capitalPoolWarning ? "<span style='color:green'>" + item.capitalPoolWarning + "</span>" : "<span style='color:orange'>" + item.nowCapitalPoolWarning + "</span>") : "<span style='color:red'>0</span>";
//                     $(obj).append("<tr>\n" +
//                         "<td>" + item.signOrgName + "</td>\n" +
//                         "<td>" + item.capitalPoolWarning + "</td>\n" +
//                         "<td>" + nowCapitalPoolWarning + "</td>\n" +
//                         "<td>" + item.receiptPrice + "</td>\n" +
//                         "<td>" + item.certificatePrice + "</td>\n" +
//                         "</tr>");
//                 });
//                 $(obj).append("</tbody>");
//             } else {
//                 $(obj).append("<tr>\n" +
//                     "<td colspan='5'><center>没有数据</center></td>\n" +
//                     "</tr></tbody>");
//             }
//
//         } else {
//             DGG.Errors(result.msg);
//             return false;
//         }
//     });
// }


/**
 * 证书总览数据跳转页面
 * @param type 跳转类型 必须传值
 */
function goToGeneralCredentail(type, val, val2) {
    var params = "";
    var name = "";
    switch (type) {
        case 0: // 录入证书量
            name="录入证书量";
            params = params + "?createTime=" + val;
            break;
        case 1: // 证书总量
            name="证书总量";
            break;
        case 2: // 证书预定量
            name="证书预定量";
            params = params + "?generalCredentialStatus=" + val;
            break;
        case 3: // 已使用证书量
            name="已使用证书量";

            params = params + "?generalCredentialStatus=" + val;
            break;
        case 4: // 使用超期总量
            name="使用超期总量";

            params = params + "?generalCredentialStatus=" + val + "&overdueType=" + val2;
            break;
        case 5: // 未使用证书总量
            name="未使用证书总量";

            params = params + "?generalCredentialStatus=" + val + "&libraryStatus=" + val2;
            break;
        case 6: // 人才合同到期量
            name="人才合同到期量";

            params = params + "?overdueType=" + val;
            break;
        case 7: // 企业合同到期量
            name="企业合同到期量";

            params = params + "?overdueType=" + val;
            break;
        case 8: // 证书过期总量
            name="证书过期总量";

            params = params + "?overdueType=" + val;
            break;
        case 9: // 全国适用证书量
            name="全国适用证书量";

            params = params + "?applicationScopes=" + val;
            break;
        case 10: // 本地适用证书量
            name="本地适用证书量";

            params = params + "?applicationScopes=" + val;
            break;
    }

    window.parent.updateTab("证书信息列表：" + name, sysInfo.basePath + "/zs/general_credential/general_certificate_info.html" + params);
}

function turnToWarningPage() {
    window.parent.updateTab("证书库容总览", sysInfo.basePath + "/zs/credential/stotage_capacity/warning_page_by_type.html");
}

/**
 * 设置资金池额度
 */
function setCapitalPoolWarning() {
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/warning/aptitude/set_captial_pool_warning_page.do", {}, function (result) {
        var index = dgg.Open({
            type: 1,
            title: '库容预警设置',
            btn: ["确定", "取消"],
            content: result,
            area: '500px',
            yes: function () {
                DGG.loading();
                if (!$('[name="capitalPoolWarning"]').attr("disabled")) {
                    if (!numCheck('[name="capitalPoolWarning"]', 8, true)) {
                        DGG.loading('close');
                        return false;
                    }
                    $.ajax({
                        type: "post",
                        url: sysInfo.basePath + "/zs/warning_configure/update_single_warning.do",
                        data: {
                            warningType: $("[name='generalType']").val(),
                            warningName: "CAPITAL_POOL_WARNING",
                            warningValue: $("[name='capitalPoolWarning']").val()
                        },
                        dataType: "json",
                        success: function (result) {
                            if (result.code == 0) {
                                layer.close(index);
                                DGG.Succeed('保存成功!');
                                DGG.loading('close');
                                loadWarningConfigure('[name="warningConfigure"]', $("[name='sybId']").val());
                            } else {
                                DGG.Errors(result.msg);
                                DGG.loading('close');
                            }
                        }
                    });

                } else {
                    DGG.Errors("您所在的组织机构没有所在事业部，不能进行设置");
                    layer.close(index);
                }

            }, end: function () {
                layer.closeAll("tips");
                DGG.loading('close');
            }
        });
    }, 'html');
}